#include "file.h"
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QDir>


file::file(QObject *parent)
    : QObject{parent}
{

}

file::~file()
{

}

void file::setFilePath(QString filePath)
{
    if(filePath == this->m_filePath) {
        return ;
    }
    this->m_filePath = filePath;
    emit filePathChanged(this->m_filePath);
}

QString file::getFilePath()
{
    return this->m_filePath;
}

void file::setFileData(QByteArray fileData)
{
    this->m_fileData = fileData;
}

QByteArray file::getFileData()
{
    return this->m_fileData;
}

void file::clearFileData()
{
    this->m_fileData.clear();
}

void file::setFileList(QStringList fileList)
{
    this->m_fileList = fileList;
}

QStringList file::getFileList()
{
    return this->m_fileList;
}

void file::clearFileList()
{
    this->m_fileList.clear();
}

bool file::importFileVideo()
{
    QString filePath = QFileDialog::getOpenFileName(NULL,tr("选择视频文件"),"",tr("Video (*.WMV *.mp4 *.rmvb *.flv)"));
    if(filePath.isEmpty())
    {
        qDebug() << "not choose file";
        return false;
    }
    setFilePath(filePath);
//    qDebug() << filePath;
    return true;
}

bool file::importFramePicture()
{
    QString filePath = QFileDialog::getOpenFileName(NULL,tr("选择帧图片"),"",tr("Images (*.png *.jpg *.bmp)"));
    if(filePath.isEmpty())
    {
        qDebug() << "not choose file";
        return false;
    }
    filePath.remove(filePath.split("/").constLast());
    setFilePath(filePath);

    QDir dir(filePath);
    QStringList nameFilters;
    nameFilters << "*.jpg" << "*.png" << "*.bmp";
    QStringList fileList;
    fileList = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
    setFileList(fileList);

    return true;
}

bool file::importFileLD()
{
    QString filePath = QFileDialog::getOpenFileName(NULL,tr("选择ld文件"),"",tr("*.ld"));
    if(filePath.isEmpty())
    {
        qDebug() << "not choose file";
        return false;
    }
    setFilePath(filePath);

    QFile file(filePath);
    if(!file.open(QIODevice::ReadWrite))
    {
       qDebug() << "open file failed";
       return false;
    }

    QByteArray byteArr;
    byteArr.clear();
    byteArr = file.read(file.size());
    file.close();
    setFileData(byteArr);

    return true;
}

bool file::createFileLD(QByteArray writeData)
{
    QString fileName = QFileDialog::getSaveFileName(NULL,tr("保存文件"),"",tr("*.ld;;")); // 选择路径
    if(fileName.isEmpty())
    {
        return false;
    }

    QFile file(fileName); // 此路径下没有就会自己创建一个
    if(!file.open(QIODevice::ReadWrite))
    {
       QMessageBox::warning(NULL, "file error", "<font size=\"4\">文件保存错误, 请重新保存</font>");
       return false;
    }
    qDebug() << "file byte size:" << writeData.size();
    file.write(writeData);
    file.close();

    return true;
}

bool file::importFileBin()
{
    QString filePath = QFileDialog::getOpenFileName(NULL,tr("选择bin文件"),"",tr("*.bin"));
    if(filePath.isEmpty())
    {
        qDebug() << "not choose file";
        return false;
    }
    setFilePath(filePath);

    QFile file(filePath);
    if(!file.open(QIODevice::ReadWrite))
    {
       QMessageBox::warning(NULL, "file error", "<font size=\"4\">文件打开失败, 请重新尝试</font>");
       return false;
    }

    QByteArray byteArr;
    byteArr.clear();
    byteArr = file.read(file.size());
    file.close();
    setFileData(byteArr);

    return true;
}
